//
//  ProgressImageView.swift
//  HMWeibo
//
//  Created by wulei on 15/12/20.
//  Copyright © 2015年 wulei. All rights reserved.
//

import UIKit

class ProgressImageView: UIImageView {
    
    var progress: CGFloat = 0 {
        
        didSet{
        progressView.progress = progress
        }
    }

    // 构造函数
    init() {
        super.init(frame: CGRectZero)
        setupUI()
    }
    


    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        addSubview(progressView)
        progressView.backgroundColor = UIColor.clearColor()

        
        
        // 设置约束
        progressView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.snp_edges)
        }
    }
    
    private lazy var progressView: ProgressView = ProgressView()
}


// MARK: - 进度视图
private class ProgressView: UIView {
    
    var progress: CGFloat = 0 {
        
        didSet{
            setNeedsDisplay()
        }
    }
    
    
    override func drawRect(rect: CGRect) {
        let center = CGPoint(x: rect.width * 0.5, y: rect.height * 0.5)
        let r = min(rect.width * 0.5, rect.height * 0.5)
        let start = CGFloat(-M_PI_2)
        let end = start + progress * 2 * CGFloat(M_PI)
        
        

        let path = UIBezierPath(arcCenter: center, radius: r, startAngle: start, endAngle: end, clockwise: true)
        path.addLineToPoint(center)
        path.closePath()
        
        UIColor(white: 1.0, alpha: 0.3).setFill()
        path.fill()
    }
}
